package com.bbs.gui.action.user;

import com.bbs.gui.form.URegisterForm;
import com.bbs.gui.form.UEditForm;
import com.bbs.gui.form.ULoginForm;
import com.bbs.gui.iface.UserIface;
import com.bbs.db.pers.Userinfo;
import com.bbs.logic.iface.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import java.util.Map;

/**
 * @time Nov 13, 2009, 12:23:02 AM
 * @author fdcube
 */
public class UserAction extends ActionSupport implements UserIface {

    private Map session = ActionContext.getContext().getSession();
    private String returnUrl;

    public String getReturnUrl() {
        return returnUrl;
    }

    public void setReturnUrl(String returnUrl) {
        this.returnUrl = returnUrl;
    }
    private URegisterForm registerForm;

    public URegisterForm getRegisterForm() {
        return registerForm;
    }

    public void setRegisterForm(URegisterForm registerForm) {
        this.registerForm = registerForm;
    }
    private ULoginForm loginForm;

    public ULoginForm getLoginForm() {
        return loginForm;
    }

    public void setLoginForm(ULoginForm loginForm) {
        this.loginForm = loginForm;
    }
    private UEditForm editForm;

    public UEditForm getEditForm() {
        return editForm;
    }

    public void setEditForm(UEditForm editForm) {
        this.editForm = editForm;
    }
    private UserService userService;

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public String Login() {
        List<Userinfo> utmp = null;
        try {
            utmp = userService.GetUser(new Userinfo(loginForm.getUname(), loginForm.getUpwd()));
            session.put("uid", utmp.get(0).getUid());
            session.put("uname", utmp.get(0).getUname());
            session.put("uabout", utmp.get(0).getUabout());
            session.put("uemail", utmp.get(0).getUemail());
            session.put("status", utmp.get(0).getUstatus());
            return SUCCESS;
        } catch (Exception e) {
            this.addActionError("登录失败，用户名或密码错误！");
            return ERROR;
        }
    }

    public String Logout() {
        try {
            session.clear();
            return SUCCESS;
        } catch (Exception e) {
            return ERROR;
        }
    }

    public String Register() {
        if (registerForm.getUabout().equals("")) {
            registerForm.setUabout("这家伙很懒，什么都没有留下。");
        }
        if (userService.Register(new Userinfo(registerForm.getUname(), registerForm.getUpwd(), registerForm.getUemail(), registerForm.getUabout()))) {
            this.addActionMessage("注册成功，请登录");
            return "reok";
        } else {
            return ERROR;
        }
    }
    public int edit = 0;

    public int getEdit() {
        return edit;
    }

    public void setEdit(int edit) {
        this.edit = edit;
    }

    public String Edit() {
        if (userService.GetUser(new Userinfo(editForm.getUname(), editForm.getUpwd())).size() < 1) {
            this.addActionError("旧密码错误，请重新输入。");
            return ERROR;
        } else {
            if (userService.UpdateUser(new Userinfo((Integer) session.get("uid"), editForm.getUname(), editForm.getNpwd(), editForm.getUemail(), editForm.getUabout()))) {
                return SUCCESS;
            } else {
                this.addActionError("服务器错误。");
                return ERROR;
            }
        }
    }

    public String SEdit() {
        try {
            editForm = new UEditForm();
            Userinfo utmp = userService.GetUser(new Userinfo((Integer) session.get("uid"))).get(0);
            editForm.setUname(utmp.getUname());
            editForm.setUabout(utmp.getUabout());
            editForm.setUemail(utmp.getUemail());
            System.out.println("Edit Returned");
            return "edit";
        } catch (Exception e) {
            System.out.println(e);
            return ERROR;
        }
    }
}
